package com.security.it.web.controller;

import com.google.common.collect.Lists;
import com.security.it.dto.User;
import com.security.it.exception.UserNotException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/page")
    public List<User> queryPage(@Valid User user) {
        List<User> users = Lists.newArrayList();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }

    @GetMapping("/{id}")
    public User queryUserInfoById(@PathVariable("id") String id) {
        //throw new UserNotException(id);
        User user = new User();
        user.setUserName("xll");
        user.setPassword("asdasdas");
        return user;
    }

    @GetMapping("/search")
    public User search() {
        User user = new User();
        user.setUserName("xll");
        user.setPassword("asdasdas");
        return user;
    }

    @GetMapping("/me")
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails user) {
        return user;
    }

    @GetMapping("/me2")
    public Object getCurrentUser2(Authentication authentication) {
        return authentication;
    }
}
